using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

public partial class GameCoreAssetManager : MonoSingleton<GameCoreAssetManager>
{
    private Dictionary<string,Sprite[]> SpriteCache = new Dictionary<string, Sprite[]>();
    private HashSet<string> SpriteCacheHaset = new HashSet<string>();                                                       

    
    public Sprite GetSprite(string spriteKey,int index)
    {
        return SpriteCache[spriteKey][index];
    }
    
    public bool CheckSprite(string spriteKey)
    {
        return SpriteCache.ContainsKey(spriteKey);
    }
    
    public Sprite[] GetSprites(string spriteKey)
    {
        Debug.Log(SpriteCache[spriteKey]);
        return SpriteCache[spriteKey];
    }
    
    public async void LoadSprite(string spriteName,int count,Action loadFinish)
    {
        if(SpriteCacheHaset.Contains(spriteName))
        {
            UtilityMain.SafePostEvent(loadFinish);
            return;
        }
        SpriteCacheHaset.Add(spriteName);
        if(!SpriteCache.ContainsKey(spriteName))
            SpriteCache.Add(spriteName,new Sprite[count]);
        var finishCont = 0;
        var atlusName = spriteName;
        for (var i = 0; i < count; i++)
        {
            string loaded_spriteName = spriteName + "_"+ i.ToString();
            var sprite = await AssetManager.Instance.LoadSpriteAsync(atlusName,loaded_spriteName);
            if(this == null)return;
            finishCont ++;
            var index = i;
            if(sprite != null)
            {
                SpriteCache[spriteName][index] = sprite;
            }
            if(finishCont == count)UtilityMain.SafePostEvent(loadFinish);
        }
    }
}